{include file="public/header" /}
<link rel="stylesheet" href="/static/admin/addons/builder/css/base.css" media="all">
<style>
	.layui-icon-file {
		display: none;
	}
</style>
<body>
<div class="layui-fluid layui-anim layui-anim-downbit">
	<div class="layui-card">
		<div class="layui-card-body">
			<form class="layui-form " id="form">
				<div class="layui-row">
					<div class="layui-col-xs12">
						<div class="layui-form-item ">
							<label class="layui-form-label">权限：</label>
							<div class="layui-input-block">
								<div id="rules"></div>
							</div>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
<div style="clear: both;height: 300px;"></div>
<div class="layui-footer" style="left: 0;">
	<button class="layui-btn builderSubmit" lay-filter="saveRole" lay-submit formObj="#form">
		保存
	</button>
</div>
</body>
{include file="public/footer" /}
<script>
	$(function () {
		var a = $, tree = layui.tree, f = layui.form;
		tree.render({
			elem: '#rules',
			id: 'rules',
			data: JSON.parse('<?php echo json_encode($list);?>'),
			showCheckbox: true,
		});

		f.on('submit(saveRole)', function () {
			var list = tree.getChecked('rules'), ids = [];
			if (list.length > 0) {
				list.forEach(function (v, i) {
					ids.push(v.id);
					v.children.forEach(function (v1, i1) {
						ids.push(v1.id);
						v1.children.forEach(function (v2, i2) {
							ids.push(v2.id);
						});
					});
				});
			}
			layui.listen.method.load();
			a.post('{:url("role/give")}', {rules: ids, id: '{$Request.get.id}'}, function (data) {
				layui.layer.closeAll();
				notify.success(data.msg, 1000, function () {
					if(parent.parent.$('body').find('.show-ref').length) {
						parent.parent.location.reload();
					} else {
						parent.layer.closeAll();
						if (parent.$(".layui-table-page").length > 0) {
							parent.layui.table.reload('form_table', {
								page: {
									curr: parent.$(".layui-laypage-em").next().text()
								},
							});
						} else {
							var w = window.frames.frameElement.id.includes('layui-layer-iframe');
							w ? parent.location.reload() : location.reload();
						}
					}
				});
			});
		});
	});
</script>
</html>